package cn.service.impl;

import cn.entity.Statute;
import cn.entity.SysResource;
import cn.entity.SysRole;
import cn.entity.SysUser;
import cn.mapper.SysRoleMapper;
import cn.service.StatuteService;
import cn.service.SysRoleService;
import cn.service.SysUserService;
import com.alibaba.druid.support.json.JSONUtils;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional//开启事务
public class SysRoleServiceImpl extends BaseServiceImpl<SysRole> implements SysRoleService {

    @Autowired
    SysRoleMapper sysRoleMapper;

    public PageInfo<SysRole> selectPage(int pageNum, int pageSize, Map<String, Object> map){
        PageHelper.startPage(pageNum,pageSize);
        SysRole sysRole = new SysRole();
        sysRole.setDelFlag("0");
        return new PageInfo<SysRole>(sysRoleMapper.selectPage(map));
    }

    @Override
    public List<SysRole> selectFromRole(Long id) {
        return sysRoleMapper.selectFromRole(id);
    }

    @Override
    public List<SysUser> selectElse(Long uid, Long oid) {
        return sysRoleMapper.selectElse(uid,oid);
    }

    @Override
    public int addBtnSubmit(Long rid,List<Long> uids) {
        return sysRoleMapper.addBtnSubmit(rid,uids);
    }

    @Override
    public int removeBtnSubmit(Long rid, List<Long> uids) {
        return sysRoleMapper.removeBtnSubmit(rid,uids);
    }

    @Override
    public int updateByPrimaryKeySelective(Map<String, Object> map) {
        int result=0;
        String str = (String) map.get("role");
        SysRole sysRole = JSON.parseObject(str,SysRole.class);
        sysRole.setUpdateDate(new Date());
        List<Long> resourcesId=null;
        List<Long> newResourcesId=null;
        resourcesId= (List<Long>) map.get("resourcesId");
        newResourcesId= (List<Long>) map.get("newResourcesId");
        //resource更新
        if (resourcesId!=null){
            if (newResourcesId!=null){
                if (resourcesId.size()==newResourcesId.size()&&resourcesId.containsAll(newResourcesId)){
                }else {
                    result+=sysRoleMapper.deleteBatchResource(sysRole.getId());
                    result+=sysRoleMapper.insertBatchResource(sysRole.getId(),newResourcesId);
                }
            }else {
                result+=sysRoleMapper.deleteBatchResource(sysRole.getId());
            }
        }else {
            if (newResourcesId!=null){
                result+=sysRoleMapper.insertBatchResource(sysRole.getId(),newResourcesId);
            }
        }

        //office更新
        if (sysRole.getDataScope().equals("9")){
            List<Long> officeId=null;
            List<Long> newOfficeId=null;
            if (map.containsKey("officeId")){
                officeId= (List<Long>) map.get("officeId");
            }
            if (map.containsKey("newOfficeId")){
                newOfficeId= (List<Long>) map.get("newOfficeId");
            }
            if (officeId==null){
                if (newOfficeId!=null){
                    result+=sysRoleMapper.insertBatchOffice(sysRole.getId(),newOfficeId);
                }
            }else {
                if (newOfficeId!=null){
                    if (officeId.size()==newOfficeId.size()&&officeId.containsAll(newOfficeId)){

                    }else {
                        result+=sysRoleMapper.deleteBatchOffice(sysRole.getId());
                        result+=sysRoleMapper.insertBatchOffice(sysRole.getId(),newOfficeId);
                    }
                }else {
                    result+=sysRoleMapper.deleteBatchOffice(sysRole.getId());
                }
            }
        }
        result += sysRoleMapper.updateByPrimaryKeySelective(sysRole);
        return result;
    }

    @Override
    public List<Long> selectOfficeByRid(Long rid) {
        return sysRoleMapper.selectOfficeByRid(rid);
    }

}
